package com.example.gss.ServiceImpl;

import com.example.gss.Service.ScoreCalculationService;
import com.example.gss.entity.ScholarshipApplication;
import com.example.gss.enums.GradeType;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 评分计算服务实现类
 */
@Service
public class ScoreCalculationServiceImpl implements ScoreCalculationService {

    @Override
    public BigDecimal calculateTotalScore(ScholarshipApplication application, GradeType gradeType) {
        if (gradeType == null) {
            return BigDecimal.ZERO;
        }
        ScoreLimits limits = getScoreLimits(gradeType);
        switch (gradeType) {
            case GRADUATE_1:
                // 研一：研究生院统一评定，这里暂时返回0，需要后续实现
                return BigDecimal.ZERO;
            case GRADUATE_2:
                // 研二：学业成绩(20分) + 思政表现(30分) + 科研能力(30分) + 社会服务(20分)
                BigDecimal academicScore = application.getAcademicScore() != null ? 
                    application.getAcademicScore() : BigDecimal.ZERO;
                BigDecimal ideologyScore = application.getIdeologyScore() != null ? 
                    application.getIdeologyScore() : BigDecimal.ZERO;
                BigDecimal researchScore = application.getResearchScore() != null ? 
                    application.getResearchScore() : BigDecimal.ZERO;
                BigDecimal serviceScore = application.getServiceScore() != null ? 
                    application.getServiceScore() : BigDecimal.ZERO;
                // 裁剪到上限
                academicScore = academicScore.min(limits.getAcademicLimit());
                ideologyScore = ideologyScore.min(limits.getIdeologyLimit());
                researchScore = researchScore.min(limits.getResearchLimit());
                serviceScore = serviceScore.min(limits.getServiceLimit());
                return academicScore.add(ideologyScore).add(researchScore).add(serviceScore);
            case GRADUATE_3:
                // 研三：思政表现(30分) + 科研能力(50分) + 社会服务(20分)
                ideologyScore = application.getIdeologyScore() != null ? 
                    application.getIdeologyScore() : BigDecimal.ZERO;
                researchScore = application.getResearchScore() != null ? 
                    application.getResearchScore() : BigDecimal.ZERO;
                serviceScore = application.getServiceScore() != null ? 
                    application.getServiceScore() : BigDecimal.ZERO;
                // 裁剪到上限
                ideologyScore = ideologyScore.min(limits.getIdeologyLimit());
                researchScore = researchScore.min(limits.getResearchLimit());
                serviceScore = serviceScore.min(limits.getServiceLimit());
                return ideologyScore.add(researchScore).add(serviceScore);
            default:
                return BigDecimal.ZERO;
        }
    }

    @Override
    public boolean validateScoreLimits(ScholarshipApplication application, GradeType gradeType) {
        if (gradeType == null) {
            return false;
        }
        
        ScoreLimits limits = getScoreLimits(gradeType);
        
        BigDecimal ideologyScore = application.getIdeologyScore() != null ? 
            application.getIdeologyScore() : BigDecimal.ZERO;
        BigDecimal academicScore = application.getAcademicScore() != null ? 
            application.getAcademicScore() : BigDecimal.ZERO;
        BigDecimal researchScore = application.getResearchScore() != null ? 
            application.getResearchScore() : BigDecimal.ZERO;
        BigDecimal serviceScore = application.getServiceScore() != null ? 
            application.getServiceScore() : BigDecimal.ZERO;
        
        // 检查各单项分数是否超过上限
        if (ideologyScore.compareTo(limits.getIdeologyLimit()) > 0) {
            return false;
        }
        if (academicScore.compareTo(limits.getAcademicLimit()) > 0) {
            return false;
        }
        if (researchScore.compareTo(limits.getResearchLimit()) > 0) {
            return false;
        }
        if (serviceScore.compareTo(limits.getServiceLimit()) > 0) {
            return false;
        }
        
        return true;
    }

    @Override
    public String getScoreRuleDescription(GradeType gradeType) {
        if (gradeType == null) {
            return "未知年级";
        }
        return gradeType.getRuleDescription();
    }

    @Override
    public ScoreLimits getScoreLimits(GradeType gradeType) {
        if (gradeType == null) {
            return new ScoreLimits(BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO);
        }
        
        switch (gradeType) {
            case GRADUATE_1:
                // 研一：研究生院统一评定，暂时设置所有上限为0
                return new ScoreLimits(
                    BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO
                );
                
            case GRADUATE_2:
                // 研二：学业成绩(20分) + 思政表现(30分) + 科研能力(30分) + 社会服务(20分)
                return new ScoreLimits(
                    new BigDecimal("30"), new BigDecimal("20"), 
                    new BigDecimal("30"), new BigDecimal("20")
                );
                
            case GRADUATE_3:
                // 研三：思政表现(30分) + 科研能力(50分) + 社会服务(20分)
                return new ScoreLimits(
                    new BigDecimal("30"), BigDecimal.ZERO, 
                    new BigDecimal("50"), new BigDecimal("20")
                );
                
            default:
                return new ScoreLimits(BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO);
        }
    }
} 